﻿/*
 *  Created on: 2013-11-14
 *      Author: admin
 */

#ifndef LOG_MACRO_H_
#define LOG_MACRO_H_

//定义一些简易的日志宏
#include <iostream>
#include <string>
#include <ctime>

#if _MSC_VER < 1600 //vc10
#define  __inline__ __inline
#endif

///
/// \brief get_file_name_in_path
/// 从一个路径中获得文件名部分
/// \param file_path
/// \return file name only
///
__inline std::string get_file_name_in_path(const std::string file_path)
{
    size_t pos=file_path.find_last_of("/");
    if(pos==std::string::npos)
        return file_path;

    return file_path.substr(pos+1);
}

#ifdef _DEBUG
#define TINY_LOG(msg)			\
{								\
	std::time_t t = std::time(0);			\
	char local_time[64];		\
    strftime(local_time, sizeof(local_time), "%Y%m%d-%X", std::localtime(&t));	\
    /*std::cout << "["<<local_time<<" "<< get_file_name_in_path(__FILE__) <<":"<<__LINE__<<"("<< __FUNCTION__ <<")]"<< msg << "\r\n";*/ \
    std::cout << "["<<local_time<<" "<< get_file_name_in_path(__FILE__) <<":"<<__LINE__<<"]"<< msg << "\r\n";	\
}
#else
#define TINY_LOG(msg)			\
{								\
	std::time_t t = std::time(0);			\
	char local_time[64];		\
    strftime(local_time, sizeof(local_time), "%Y%m%d-%X", std::localtime(&t));	\
    std::cout << "["<<local_time<<"]"<< msg << "\r\n";	\
}
#endif

#ifdef	_DEBUG
#define	TDEBUG(msg)	TINY_LOG("<dbg> " << msg)
#else
#define	TDEBUG(msg)
#endif	//_DEBUG
#define	TINFO(msg)	TINY_LOG("<info> " << msg)
#define	TWARN(msg)	TINY_LOG("<warn> " << msg)
#define	TERROR(msg)	TINY_LOG("<err> " << msg)

#endif /* LOG_MACRO_H_ */
